﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using HYCodeBase.WinForm;
using HYView;

namespace ElectricDim
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdWireNumDim : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			ElectricalUtils.ViewValidating(document);
			WindowHandle windowHandle = null;
			if (windowHandle == null)
			{
				windowHandle = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			DlgWireNumDim instance = DlgWireNumDim.GetInstance(commandData.Application, windowHandle);
			instance.Show(windowHandle);
			new SubTransaction(activeUIDocument.Document);
			for (;;)
			{
				Transaction transaction = new Transaction(document, "LayLabel");
				try
				{
					if (instance.Num < 2)
					{
						MessageBox.Show("请填写大于1的根数。");
						instance.Num = 2;
						instance.Close();
					}
					else
					{
						transaction.Start();
						Reference reference = activeUIDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new WireFilter());
						Element element = document.GetElement(reference);
						if (element != null)
						{
							try
							{
								ElementIdDate elementIdDate = Assist.ExtensibleStorageManager.ReadFromElement<ElementIdDate>(element, this.guid);
								if (elementIdDate.GetId().IntegerValue != 0)
								{
									document.Delete(elementIdDate.GetId());
								}
							}
							catch
							{
							}
							(element as Wire).HotConductorNum = instance.Num;
							(element as Wire).GroundConductorNum = 1;
							(element as Wire).NeutralConductorNum = 1;
							if (instance.Num > 3)
							{
								element.GetParameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).Set(instance.Num.ToString());
							}
							else if (instance.Num < 2)
							{
								MessageBox.Show("请填写大于1的根数。");
								instance.Num = 2;
								transaction.RollBack();
								continue;
							}
							double angle = ElectricalUtils.PromtAngle(reference.GlobalPoint, element as Wire);
							Autodesk.Revit.DB.ElementType elementType = instance.GetElementType(angle);
							if (elementType != null)
							{
								IndependentTag independentTag = RevitVersionFuncs.NewTag(document, document.ActiveView, new Reference(element), false, 0, 0, reference.GlobalPoint);
								independentTag.ChangeTypeId(elementType.Id);
								transaction.Commit();
								ElementIdDate obj = new ElementIdDate(independentTag.Id);
								Assist.ExtensibleStorageManager.WriteToElement(element, this.guid, obj);
								continue;
							}
							MessageBox.Show("没有找到标记族。");
							transaction.RollBack();
							return Autodesk.Revit.UI.Result.Failed;
						}
					}
				}
				catch (Exception)
				{
					instance.Close();
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				break;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Guid guid = new Guid("356cda9a-e6c5-4c2f-a9ef-b1313131b8c8");
	}
}
